import ProxyHandler from "../sql/ProxyHandler";
import {BrowserWindow} from "electron";
import {v4 as uuidv4} from 'uuid';
import md5 from "crypto-js/md5";
import SystemProductCategoryMapper from "../dao/SystemProductCategoryMapper";
import SystemProductCategory from "../entity/SystemProductCategory";

export default class SystemProductCategoryService {
    private mapper: SystemProductCategoryMapper;
    private handler: ProxyHandler<SystemProductCategory>;

    constructor() {
        this.mapper = new SystemProductCategoryMapper()
        this.handler = new ProxyHandler<SystemProductCategory>();
    }

    async page(data){
        const win = BrowserWindow.getFocusedWindow();
        if(win == null){
            throw new Error("应用程序错误")
        }
        let offset = 0, limit = 10
        if(data.current && data.size){
            offset = (data.current - 1) * data.size
            limit = data.size
        }

        let params = {
            tableName: 'system_product_category',
            offset: offset,
            limit: limit,
            entity: [{
                name: 'name',
                value: data.name,
                operator: 'LIKE'
            }]
        }

        const proxy = new Proxy(this.mapper.page, this.handler)
        const { rows, total } = await proxy(params)

        let result = {
            records: rows ? rows : [],
            total: total ? total : 0,
            current: data.current ? data.current : 1,
            size: limit,
            pages: Math.ceil(total / limit)
        }
        win.webContents.send('main-to-renderer', {
            name: 'category-page-success',
            event: 'event',
            data: result
        })
    }

    async save(data){
        const win = BrowserWindow.getFocusedWindow();
        if(win == null){
            throw new Error("应用程序错误")
        }
        const grade = new SystemProductCategory();
        grade.id = md5(uuidv4()).toString();
        grade.name = data.name;
        grade.enabled = data.enabled;
        const proxy = new Proxy(this.mapper.insert, this.handler)
        proxy(grade)
        win.webContents.send('main-to-renderer', {
            name: 'category-operator-success',
            event: 'event',
            data: '操作成功'
        })
    }

    async updateById(data){
        const win = BrowserWindow.getFocusedWindow();
        if(win == null){
            throw new Error("应用程序错误")
        }
        const grade = new SystemProductCategory();
        grade.id = data.id;
        grade.name = data.name;
        grade.enabled = data.enabled;
        const proxy = new Proxy(this.mapper.updateById, this.handler)
        proxy(grade)
        win.webContents.send('main-to-renderer', {
            name: 'category-operator-success',
            event: 'event',
            data: '操作成功'
        })
    }

    async deleteById(data){
        const win = BrowserWindow.getFocusedWindow();
        if(win == null){
            throw new Error("应用程序错误")
        }
        const grade = new SystemProductCategory();
        grade.id = data;
        const proxy = new Proxy(this.mapper.deleteById, this.handler)
        proxy(grade)
        win.webContents.send('main-to-renderer', {
            name: 'category-operator-success',
            event: 'event',
            data: '操作成功'
        })
    }

    async all(){
        const win = BrowserWindow.getFocusedWindow();
        if(win == null){
            throw new Error("应用程序错误")
        }
        const list = await this.mapper.all()
        win.webContents.send('main-to-renderer', {
            name: 'category-all-success',
            event: 'event',
            data: list
        })
    }
}
